﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Roles = Medianamik.Core.Security.Roles;
using Medianamik.Core.Security;
using System.Globalization;

namespace Medianamik.UI.Web.ModalWindows
{
    public partial class ValidateInterfaceWindow : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            Guid interfaceId;

            if (!StringHelper.IsGuid(Request.QueryString["interfaceid"], out interfaceId)) return;

            var interf = NodeTypeManager.GetNodeTypeInterface(interfaceId);

            if (interf == null) return; //TODO: Message d'erreur

            NodeType type = null;
            Guid? typeId = null;

            Title = interf.Name;

            if (StringHelper.IsGuid(Request.QueryString[DefaultParamNames.TypeID], ref typeId))
            {
                type = NodeTypeManager.GetNodeType(typeId.Value);

                if (type != null)
                    Title += " (" + type.Name + ")";
            }

            PropertyTable.Text = GetPropertyTable(interf, type);
        }

        private static string GetPropertyTable(NodeTypeInterface interf, NodeType type)
        {
            if (interf.Properties.Count == 0) return "";

            var tableBuilder = new StringBuilder();
            tableBuilder.AppendLine("<table><tr><td><strong>Propriété</strong></td><td><strong>Type</strong></td><td><strong>Taille maximale</strong></td><td><strong>Relation</strong></td></tr>");

            for (var i = 0; i < interf.Properties.Count; i++)
            {
                var property = interf.Properties[i];
                NodeTypeProperty typeProperty = null;
                var isSameDataType = false;
                var lengthIsOk = false;
                var lookupIsOk = false;
                var isLookup = false;

                if (type != null)
                {
                    typeProperty = type.GetAllProperties()
                        .FirstOrDefault(prop => String.Compare(prop.Name, property.Name, true) == 0);

                    if (typeProperty != null)
                        isSameDataType = typeProperty.DataTypeId == property.DataTypeId ||
                            (property.DataTypeId.Equals(DataTypes.NVarcharMax) && typeProperty.DataTypeId.Equals(DataTypes.Varchar));

                    if (isSameDataType)
                    {
                        if (typeProperty.DataType.MustSpecifyLength)
                            lengthIsOk = typeProperty.MaxLength <= property.MaxLength ||
                                (property.DataTypeId.Equals(DataTypes.NVarcharMax) && typeProperty.DataTypeId.Equals(DataTypes.Varchar));
                        else
                        {
                            if (typeProperty.DataTypeId.Equals(DataTypes.UniqueIdentifier) && property.PrimaryTypeID.HasValue)
                            {
                                isLookup = true;
                                lookupIsOk = typeProperty.PrimaryTypeID.HasValue && NodeTypeManager
                                    .GetNodeType(typeProperty.PrimaryTypeID.Value)
                                    .IsChildOf(property.PrimaryTypeID.Value);
                            }

                            lengthIsOk = true;
                        }
                    }
                }

                AddPropertyRow(tableBuilder, property, type != null, typeProperty != null,
                               isSameDataType, lengthIsOk, isLookup, lookupIsOk);
            }

            return tableBuilder.AppendLine("<table>").ToString();
        }

        private static void AddPropertyRow(StringBuilder tableBuilder, NodeTypeInterfaceProperty property,
            bool isComparison, bool isImplemented, bool isSameDataType, bool lengthIsOk, bool isLookup, bool lookupIsOk)
        {
            if (isComparison)
                tableBuilder.AppendLine(String.Format("<tr><td{0}>{1}</td><td{2}>{3}</td><td{4}>{5}</td><td{6}>{7}</td></tr>",
                    isImplemented ? "" : " class=\"TxtErreur\"", property.Name,
                    isSameDataType ? "" : " class=\"TxtErreur\"", property.DataType.SQLDataTypeName,
                    lengthIsOk ? "" : " class=\"TxtErreur\"", property.DataType.MustSpecifyLength ?
                    property.MaxLength.ToString() : "&nbsp;", !isLookup || lookupIsOk ? "" : " class=\"TxtErreur\"",
                    isLookup && property.PrimaryTypeID.HasValue ? NodeTypeManager.GetNodeType(property.PrimaryTypeID.Value).Name : "&nbsp;" ));
            else
                tableBuilder.AppendLine(String.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                    property.Name, property.DataType.SQLDataTypeName,
                    property.DataType.MustSpecifyLength ? property.MaxLength.ToString() : "&nbsp;",
                    property.PrimaryTypeID.HasValue ? NodeTypeManager.GetNodeType(property.PrimaryTypeID.Value).Name : "&nbsp;"));
        }
    }
}
